<?php

namespace App\Request\auth\Service\Auth;

use App\Exception\BusinessException;
use App\Repository\Auth\AdminRoleRepository;
use App\Repository\Auth\RoleRepository;
use App\Request\auth\Service\IService;


/**
 * 后台菜单服务
 */
final class RoleService extends IService
{
    public function __construct(
        protected readonly RoleRepository      $repository,
        protected readonly AdminRoleRepository $adminRoleRepository
    )
    {
    }


    /**
     * 创建\编辑角色
     * @param mixed $id
     * @param array $data
     * @return mixed
     */
    public function updateById(mixed $id, array $data): mixed
    {
        if ($id) {
            return parent::updateById($id, $data);
        }
        return $this->create($data);
    }


    /**
     * 删除角色
     * @throws \Exception
     */
    public function deleteById(mixed $id): int
    {
        if ($id == 1) throw new BusinessException('超级管理员角色不能删除');
        //查询角色下是否有管理员
        $count = $this->adminRoleRepository->getQuery()->where('role_id', $id)->count();
        if ($count) throw new BusinessException('该角色下有管理员，请先删除管理员');

        return parent::deleteById($id); // TODO: Change the autogenerated stub
    }

}